package com.tongji.housediy.view
{
	import com.tongji.housediy.events.designEvent.SaveDeleteDesignEvent;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.RightCompStateModel;
	import com.tongji.housediy.vo.Design;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.*;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/4/13
	 */ 
	public class MyDiyDataListMeditor extends Mediator
	{
		[Inject]
		public var view:MyDiyDataListRepeatedItem;
		
		[Inject]
		public var stateModel:RightCompStateModel;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		override public function onRegister():void
		{
			//view.designPic2.source = new Bitmap(roomModel.roomImg);
			view.designPic.addEventListener(MouseEvent.MOUSE_DOWN, openDesignHandler, false);
			//view.editDesignBtn.addEventListener(MouseEvent.MOUSE_DOWN, editDesignName);
			view.editDesignBtn.addEventListener(MouseEvent.CLICK, saveDesignNameHandler);
			view.shareDesignBtn.addEventListener(MouseEvent.MOUSE_DOWN, shareDesignHandler);
			view.releaseDesignBtn.addEventListener(MouseEvent.MOUSE_DOWN, releaseDesignHandler);
			view.deleteDesignBtn.addEventListener(MouseEvent.MOUSE_DOWN, deleteDesignHandler);
		}
		
		public function openDesignHandler(event:MouseEvent):void
		{ 
			roomModel.openHouse(new XML((view.data as Design).xmlSrc));
		}
		
		public function editDesignName(event:MouseEvent):void
		{;
			view.designNameTX.editable = true;
			view.designNameTX.addEventListener(FlexEvent.ENTER, saveDesignNameHandler);
			view.editDesignBtn.removeEventListener(MouseEvent.MOUSE_DOWN, editDesignName);
            view.editDesignBtn.addEventListener(MouseEvent.CLICK, saveDesignNameHandler);
		}
		
		public function saveDesignNameHandler(event:Event):void
		{
			Alert.show("修改成功");
			//view.designNameTX.editable = false;
			//view.editDesignBtn.addEventListener(MouseEvent.MOUSE_DOWN, editDesignName);
		}
		
		public function shareDesignHandler(event:MouseEvent):void
		{			
			navigateToURL(new URLRequest("http://www.idceye.cn/"), "_blank");
		}
		
		public function releaseDesignHandler(event:MouseEvent):void
		{
			stateModel.state = "State9";
		}
		
		public function deleteDesignHandler(event:MouseEvent):void
		{
			dispatch(new SaveDeleteDesignEvent(SaveDeleteDesignEvent.DELETE_DESIGN, view.data as Design));
		}
	}
}